local MapManage = class("MapManage")
MapManage.instance = nil

function MapManage:ctor(data)
    print("MapManage")
    self.SpritePos = {}
    self.SpriteTable = {}    
    self.MapTable = {}
    self.MapSprite = display.newNode()
    self.size = {}

    self.data = self.data or td.Map --地图设定 width = 展示宽度 , height = 展示高度 , num_x = 横向格子数 , num_y = 纵向格子数 , w = 格子宽度 , h = 格子高度
   
    self:init()
end

function MapManage:GetInstance()
	if MapManage.instance == nil then
		MapManage.instance = MapManage.new()
	end
	return MapManage.instance
end

function MapManage:init()
    self:CreateSpritePos(self.data.w,self.data.h,self.data.num_x,self.data.num_y)--单个长宽，长宽数量
--    self:CreateSprite()

end

function MapManage:CreateSpritePos(w,h,x,y)   
    self.pos = {x=0,y=0}
    self.size = {width=w*x,height=h*y}
    for i=1,x do
        self.SpritePos[i] = {}
        for t=1,y do
            self.SpritePos[i][t] = { x = w*i-w*0.5 + self.pos.x,y = h*t-h*0.5 + self.pos.y ,scaleX = w/100,scaleY = h/100 ,w=w,h=h}
        end
    end
end

function MapManage:CreateSprite()    
    local Node = display.newNode()
    for i, var in pairs(self.SpritePos) do
        if i%2==1 then 
            for t, v in pairs(var) do
                if t%2==1 then 
                    local pos = self.SpritePos[i][t]
                    local Sprite = display.newSprite("UI/tipe/2to2.png")
                    Sprite:scale(pos.scaleX,pos.scalseY)
                    Sprite:move(pos.x+pos.w*0.5,pos.y+pos.h*0.5)
                    Sprite:addTo(Node)
--                    self.MapSprite:addChild(Sprite)
    --            self.SpriteTable[i,t] 
                end
            end
        end        
    end 
    return Node
end

function MapManage:GetMap()  
    if self.Map == nil then  
        self.Map = CreateSprite()
        self:CreateSprite()
    end
    return self.Map
end

function MapManage:GetSmallMap()  
    if self.SmallMap == nil then        
        self:CreateSmallMap()
    end
    return self.SmallMap
end

function MapManage:AddToMap(children)  
    if self.Map == nil then        
        self:GetMap()
    end

    self.MapTable[children.id] = children
    children:addTo(self.Map)
end

function MapManage:CreateSmallMap()  

end

function MapManage:UpdateSmallMap()  

end

function MapManage:Update(dt)  
    if self.SmallMap then 
        self:UpdateSmallMap()
    end
end

return MapManage